﻿using UnityEditor;
using UnityEngine;
using System.IO;

namespace DA.AnimGraph.Editor {
    /// <summary>
    /// 复制aga文件的后处理
    /// </summary>
    public class CopyAGAProcesser : AssetPostprocessor {
        static void OnPostprocessAllAssets(
            string[] importedAssets,
            string[] deletedAssets,
            string[] movedAssets,
            string[] movedFromAssetPaths) {
            foreach (string assetPath in importedAssets) {
                if (assetPath.Contains(".aga 1")) {
                    var asset = AssetDatabase.LoadAssetAtPath<AnimGraphArchiveAsset>(assetPath);
                    if (asset is AnimGraphArchiveAsset agaAsset) {
                        ProcessCopiedSO(agaAsset, assetPath);
                        var fileName = Path.GetFileName(assetPath);
                        fileName = fileName.Substring(0, fileName.Length - ".aga 1.asset".Length);
                        AssetDatabase.RenameAsset(assetPath, fileName + " COPY.aga.asset");
                    }
                }
            }
        }
        private static void ProcessCopiedSO(AnimGraphArchiveAsset agaAsset, string assetPath) {
            var handle = AGAAssetHandle.Open(assetPath);
            handle.ResetSOUuid(); // 重新生成uuid
            handle.publishPath = null; // 清空导出目录
            handle.Save();
        }

    }
}